﻿using System;
using Game.Framework.Utils;
using UnityEngine;
using TMPro;

namespace Game.Framework.Localization
{
    [ExecuteInEditMode]
    [DisallowMultipleComponent]
    [RequireComponent(typeof(TextMeshProUGUI))]
    public class LocalizeTextMeshProUGUI : AbstractLocalize
    {
        [SerializeField]
        public string m_Matrial;

        private TextMeshProUGUI m_TmpText;

        /// <summary> 当前文本 </summary>
        public string cachedText => m_cachedText;
        private string m_cachedText = string.Empty;

        public TextMeshProUGUI Text
        {
            get
            {
                return m_TmpText;
            }
        }

        private void Awake()
        {
            m_TmpText = transform.GetComponent<TextMeshProUGUI>();
            m_Matrial = GetMaterialSuffix2(m_TmpText.materialForRendering.name);
            m_cachedText = m_TmpText ? m_TmpText.text : string.Empty;
        }


#if UNITY_EDITOR
        private void LateUpdate()
        {
            if (!Application.isPlaying)
                m_Matrial = GetMaterialSuffix2(m_TmpText.materialForRendering.name);
        }
#endif 

        protected override void Localize()
        {
            SetText();
        }

        public TextMeshProUGUI GetTmpText()
        {
            return m_TmpText;
        }

        private void SetText()
        {
            if (string.IsNullOrEmpty(Term)) return;

            string translation = LocalizationManager.Instance.GetLocalizedString(Term);
            if (!String.IsNullOrEmpty(translation))
            {
                SetText(translation);
            }
            else
            {
                SetText("");
                LogUtil.LogError(transform.name + " ### LocalizeTextMeshProUGUI Term error: " + Term + " ###");
            }
        }

        public void SetText(string str)
        {
            if (str == null)
                return;

            if (m_TmpText == null)
                m_TmpText = transform.GetComponent<TextMeshProUGUI>();

            if (m_TmpText != null)
            {
                m_TmpText.SetText(str);
                SetFont();
                CurrLocalize = LocalizationManager.Instance.GetCurrentLocale();
            }
            else
                LogUtil.LogError("### LocalizeTextMeshProUGUI Component error: " + str + " ###");

            m_cachedText = str;
        }

        public string GetText()
        {
            if (m_TmpText != null)
                return m_TmpText.text;
            else
            {
                LogUtil.LogError("### LocalizeTextMeshProUGUI Component error: " + gameObject.name + " ###");
                return string.Empty;
            }
        }

        public void SetColor(Color color)
        {
            if (m_TmpText != null)
                m_TmpText.color = color;
            else
                LogUtil.LogError("### LocalizeTextMeshProUGUI Component error: " + gameObject.name + " ###");
        }

        public Color GetColor()
        {
            if (m_TmpText == null)
                Awake();
            if (m_TmpText != null)
                return m_TmpText.color;

            return Color.white;
        }
        
        public void SetFont()
        {
            if (!String.IsNullOrEmpty(m_Matrial))
            {
                LocalizeFont();
            }
            LocalizeMaterial();
        }

        private void LocalizeFont()
        {
            TMP_FontAsset localizeFont = LocalizationManager.Instance.GetLocaleFont();
            if (localizeFont != null)
            {
                m_TmpText.font = localizeFont;
            }
        }

        private void LocalizeMaterial()
        {
            if (!string.IsNullOrEmpty(m_Matrial) && !m_Matrial.Equals("Material"))
            {
                Material material = LocalizationManager.Instance.GetLocaleMaterial(m_Matrial);
                if (material != null)
                    m_TmpText.fontMaterial = material;
                else
                {
                    // DebugUtil.LogError(transform.name + "  ######  " + m_Matrial);
                }
            }
        }
        
        private static string GetMaterialSuffix2(string materialName)
        {
            // LocaleFont_En SDF combo Shadow 2
            string mName = materialName;
            if(mName.EndsWith("(Instance)"))
            {
                mName = mName.Substring(0, mName.LastIndexOf("(Instance)"));                
            }
            if(mName.LastIndexOf("SDF") > -1)
            {
                mName = mName.Substring(mName.LastIndexOf("SDF") + 3);
            }
            if(mName.EndsWith(")") )
            {
                mName = mName.Substring(0, mName.LastIndexOf(")"));       
            }
            mName = mName.Trim();
            
            return mName;
        }
    }
}